/**
 *
 */
function CanvasDrawImageAssistant(inNumSprites) {

  this.numSprites = inNumSprites;

};


/**
 *
 */
CanvasDrawImageAssistant.prototype.krelmacWidth = 50;
CanvasDrawImageAssistant.prototype.krelmacHeight = 75;
CanvasDrawImageAssistant.prototype.gentooWidth = 50;
CanvasDrawImageAssistant.prototype.gentooHeight = 41;
CanvasDrawImageAssistant.prototype.krelmac = new Array(1);
CanvasDrawImageAssistant.prototype.i = null;
CanvasDrawImageAssistant.prototype.randNum1 = null;
CanvasDrawImageAssistant.prototype.randNum2 = null;
CanvasDrawImageAssistant.prototype.numSprites = null;
CanvasDrawImageAssistant.prototype.canvas = null;
CanvasDrawImageAssistant.prototype.ctx = null;
CanvasDrawImageAssistant.prototype.mainLoopInterval = null;
CanvasDrawImageAssistant.prototype.mainLoopBind = null;
CanvasDrawImageAssistant.prototype.lastFrameTime = null;
CanvasDrawImageAssistant.prototype.divFrameTime = null;
CanvasDrawImageAssistant.prototype.canvasWidth = null;
CanvasDrawImageAssistant.prototype.canvasHeight = null;
CanvasDrawImageAssistant.prototype.keypressHandlerBind = null;
CanvasDrawImageAssistant.gentoo = null;

/**
 * Variables used for various calculations.
 */
CanvasDrawImageAssistant.frameCount = null;
CanvasDrawImageAssistant.totalFrameTime = null;
CanvasDrawImageAssistant.highestFrameTime = null;


/**
 *
 */
CanvasDrawImageAssistant.prototype.setup = function() {

  if (this.canvas) { return; }

  this.keypressHandlerBind = this.keypressHandler.bind(this);
  Mojo.Event.listen(this.controller.document, Mojo.Event.keypress, 
    this.keypressHandlerBind, true);

  this.canvas = $("canvasDrawImage_canvas");
  this.ctx = this.canvas.getContext("2d");
  this.ctx.fillStyle = "rgb(255,255,255)";
  this.canvasWidth = this.canvas.getAttribute("width");
  this.canvasHeight = this.canvas.getAttribute("height");
  
  this.divFrameTime = $("canvasDrawImage_frameTime");

  for (this.i = 0; this.i < this.numSprites; this.i++) {
    this.randNum1 = Math.floor(Math.random() * 100);
    if (this.randNum1 <= this.krelmacWidth) {
      this.randNum1 = false;
    } else {
      this.randNum1 = true;
    }
    this.randNum2 = Math.floor(Math.random() * 100);
    if (this.randNum2 <= this.krelmacWidth) {
      this.randNum2 = false;
    } else {
      this.randNum2 = true;
    }
    this.krelmac[this.i] = {
      img : new Image(),
      x : Math.floor(Math.random() * (this.canvasWidth - this.krelmacWidth - 2)), 
      y : Math.floor(Math.random() * (this.canvasHeight - this.krelmacHeight - 2)),
      width : this.krelmacWidth, height : this.krelmacHeight,
      vDir : this.randNum1, hDir : this.randNum2,
      maxX : (this.canvasWidth - this.krelmacWidth - 2), 
      maxY : (this.canvasHeight - this.krelmacHeight - 2)
    };
    this.krelmac[this.i].img.src = "images/krelmac0.gif";
  }

  this.gentoo = {
    img : new Image(),
    x : 20, y : 20,
    width : this.gentooWidth, height : this.gentooHeight,
    maxX : (this.canvasWidth - this.gentooWidth - 2), 
    maxY : (this.canvasHeight - this.gentooHeight - 2)
  };
  this.gentoo.img.src = "images/gentoo0.gif";

  this.lastFrameTime = new Date().getTime();

  this.mainLoopBind = this.mainLoop.bind(this);

};


/**
 *
 */
CanvasDrawImageAssistant.prototype.activate = function() {

  this.highestFrameTime = 0;
  this.lastFrameTime = -1;
  this.frameCount = 0;
  this.totalFrameTime = 0;
  this.totalFrameProcessingTime = 0;
  this.mainLoopInterval = setInterval(this.mainLoopBind, 33);

};


/**
 *
 */
CanvasDrawImageAssistant.prototype.deactivate = function() {

  clearInterval(this.mainLoopInterval);
  this.mainLoopInterval = null;

};


/**
 *
 */
CanvasDrawImageAssistant.prototype.mainLoop = function() {

  var frameStartTime = new Date().getTime();

  this.ctx.fillRect(0, 0, this.canvasWidth, this.canvasHeight);

  for (this.i = 0; this.i < this.numSprites; this.i++) {

    this.ctx.drawImage(
      this.krelmac[this.i].img, this.krelmac[this.i].x, this.krelmac[this.i].y, 
      this.krelmac[this.i].width, this.krelmac[this.i].height
    );
  
    if (this.krelmac[this.i].hDir) {
      this.krelmac[this.i].x = this.krelmac[this.i].x + 1;
    } else {
      this.krelmac[this.i].x = this.krelmac[this.i].x - 1;
    }
    if (this.krelmac[this.i].vDir) {
      this.krelmac[this.i].y = this.krelmac[this.i].y + 1;
    } else {
      this.krelmac[this.i].y = this.krelmac[this.i].y - 1;
    }
  
    if (this.krelmac[this.i].x >= this.krelmac[this.i].maxX ||
      this.krelmac[this.i].x <= 2) {
      this.krelmac[this.i].hDir = !this.krelmac[this.i].hDir;
    }
    if (this.krelmac[this.i].y >= this.krelmac[this.i].maxY ||
      this.krelmac[this.i].y <= 2) {
      this.krelmac[this.i].vDir = !this.krelmac[this.i].vDir;
    }

  }

  // Draw Gentoo.
  this.ctx.drawImage(
    this.gentoo.img, this.gentoo.x, this.gentoo.y, 
    this.gentoo.width, this.gentoo.height
  );

  // Figure out and display frame processing time and time since last frame.
  var frameEndTime = new Date().getTime();
  this.divFrameTime.innerHTML = (frameEndTime - frameStartTime);
    
  // Update frameCount and totalFrameTime for average calculations later.
  this.frameCount = this.frameCount + 1;
  this.totalFrameTime = this.totalFrameTime + (frameEndTime - frameStartTime); 

  // Record this frame time if it's the highest observed.
  if ((frameEndTime - frameStartTime) > this.highestFrameTime) {
    this.highestFrameTime = frameEndTime - frameStartTime;
  }
  
};


/**
 *
 */
CanvasDrawImageAssistant.prototype.keypressHandler = function(inEvent) {
Mojo.Log.error("### " + inEvent.originalEvent.keyCode);
  switch (inEvent.originalEvent.keyCode) {

    case Mojo.Char.spaceBar:
      if (this.mainLoopInterval) {
        clearInterval(this.mainLoopInterval);
        this.mainLoopInterval = null;
        this.controller.showAlertDialog({
          onChoose : function(inValue) {
            this.controller.stageController.popScene(); 
          },
          title : "Report",
          message : 
            "Highest frame time: " + this.highestFrameTime + "ms ............. " +
            "Average frame time: " + 
            Math.round((this.totalFrameTime / this.frameCount)) + 
            "ms",        
          choices : [
            { label : "Ok" }    
          ]
        });
      }    
    break;

    // Up.
    case Mojo.Char.w: case Mojo.Char.w + 32:
      if (this.gentoo.y > 2) {
        this.gentoo.y = this.gentoo.y - 2;
      }
    break;
    
    // Down.
    case Mojo.Char.z: case Mojo.Char.z + 32:
      if (this.gentoo.y < this.gentoo.maxY) {
        this.gentoo.y = this.gentoo.y + 2;
      }    
    break;
    
    // Left.
    case Mojo.Char.a: case Mojo.Char.a + 32:
      if (this.gentoo.x > 2) {
        this.gentoo.x = this.gentoo.x - 2;
      }
    break;
    
    // Right.
    case Mojo.Char.d: case Mojo.Char.d + 32:
      if (this.gentoo.x < this.gentoo.maxX) {
        this.gentoo.x = this.gentoo.x + 2;
      }    
    break;
    
  }
  
};
